<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\HomeBaseController;
use app\portal\model\PortalCategoryModel;
use app\portal\service\PostService;
use app\portal\model\PortalPostModel;
use think\facade\Db;

class ArticleController extends HomeBaseController
{
    /**
     * 文章详情
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {

        $portalCategoryModel = new PortalCategoryModel();
        $postService = new PostService();

        $articleId = $this->request->param('id', 0, 'intval');
        $categoryId = $this->request->param('cid', 0, 'intval');
        $article = $postService->publishedArticle($articleId, $categoryId);

        if (empty($article)) {
            abort(404, '文章不存在!');
        }


        $prevArticle = $postService->publishedPrevArticle($articleId, $categoryId);
        $nextArticle = $postService->publishedNextArticle($articleId, $categoryId);

        $tplName = 'article';

        if (empty($categoryId)) {
            $categories = $article['categories'];

            if (count($categories) > 0) {
                $this->assign('category', $categories[0]);
            } else {
                abort(404, '文章未指定分类!');
            }

        } else {
            $category = $portalCategoryModel->where('id', $categoryId)->where('status', 1)->find();

            if (empty($category)) {
                abort(404, '文章不存在!');
            }

            $this->assign('category', $category);

            $tplName = empty($category["one_tpl"]) ? $tplName : $category["one_tpl"];
        }

        PortalPostModel::where('id', $articleId)->inc('post_hits')->update();


        hook('portal_before_assign_article', $article);

        $this->assign('article', $article);
        $this->assign('prev_article', $prevArticle);
        $this->assign('next_article', $nextArticle);

        $tplName = empty($article['more']['template']) ? $tplName : $article['more']['template'];

        return $this->fetch("/$tplName");
    }

    // 文章点赞
    public function doLike()
    {
        $this->checkUserLogin();
        $articleId = $this->request->param('id', 0, 'intval');


        $canLike = cmf_check_user_action("posts$articleId", 1);

        if ($canLike) {
            PortalPostModel::where('id', $articleId)->inc('post_like')->update();

            $this->success("赞好啦！");
        } else {
            $this->error("您已赞过啦！");
        }
    }


    /**
     * 列表
     * @return mixed
     * User: sunc
     * Date: 2021/6/22 14:03
     */
    public function list()
    {
        $categoryId = $this->request->param('id', 0, 'intval');
        $category = Db::name('portal_category')->where('delete_time', 0)->field('name,id')->select()->toArray();
        if (empty($categoryId) && !empty($category)) {
            $categoryId = $category[0]['id'];
        }
        if (empty($categoryId)) {
            $this->error("数据错误");
        }
        $art = Db::name('portal_category_post')
            ->alias('cp')
            ->leftJoin('portal_post p', 'p.id = cp.post_id')
            ->where('cp.category_id', $categoryId)
            ->field('p.post_title,p.id,p.post_excerpt,p.published_time,p.thumbnail')
            ->paginate(20);

        $this->assign('list', $art);
        $this->assign('cate', $category);
        $this->assign('page', $art->render());

        return $this->fetch('/art_list');
    }


    public function detail()
    {
        $id = $this->request->param('id', 0, 'intval');
        $data = Db::name('portal_post')->where('id', $id)->field('*')->find();
        if (empty($data)) {
            return $this->error('数据错误');
        }
        $data['post_content'] = cmf_replace_content_file_url(htmlspecialchars_decode($data['post_content']));
        $this->assign('data', $data);
        return $this->fetch('/detail');
    }

}
